	<?php
	/* configuration */
	error_reporting(0);
	$status = array("Free", "Active", "Suspended", "Terminated", "Pending", "Cancelled");
	//$ajax_url = "https://secure.vinahost.vn/ac/modules/admin/ip_manager/ajax.php";
	$ajax_url = "http://vndev.n3txpert.net/whmcs/modules/admin/ip_manager/ajax.php";
	$ajax_remove_url = "#";

	/* functions */
	function log_ip_history($ipid, $action)
	{
		return mysql_query("INSERT INTO mod_history (id, tid, uid, logtime, action, type) VALUES ('', '".intval($ipid)."', '".intval($_SESSION["adminid"])."', NOW(), '".mysql_real_escape_string($action)."', 'iplog');");
	}

	function get_ip_last_oct($ip) 
	{
		$tmp = explode(".", $ip);
		return $tmp[3];
	}

	function get_ip_first3_oct($ip)
	{
		$tmp = explode(".", $ip);
		return $tmp[0].".".$tmp[1].".".$tmp[2];
	}

	/* basic authentication check */
	if (!isset($_SESSION["adminid"]) || !defined('ROOTDIR'))
	{
		exit();
	}

	// permission to view client products required for access
	$permid = 11;
	if (!mysql_num_rows(mysql_query("SELECT permid FROM tbladmins JOIN tbladminperms ON tbladmins.roleid = tbladminperms.roleid WHERE permid=".$permid." AND tbladmins.id=". intval($_SESSION["adminid"]).";")))
		exit();

	/* JS and CSS */
	?>
	<style>
	  table.datatable tr.norm td{ background-color: #FFF }
	  table.datatable tr.over td{ background-color: #AAA }
	  #ipStart, #ipEnd{color: gray; font-size: 0.8em, font-style: italic}
	</style>
	<script type="text/javascript" src="../modules/admin/developer/ibox/ibox.js"></script>
	<script type="text/javascript">
	$(document).ready(function()
	{
		$(".tabbox").css("display","none");
		var selectedTab;
		$(".tab").click(function()
		{
			var elid=$(this).attr("id");
			$(".tab").removeClass("tabselected");
			$("#"+elid).addClass("tabselected");
			$(".tabbox").slideUp();
			if (elid!=selectedTab)
			{
				selectedTab=elid;
				$("#"+elid+"box").slideDown();
			}
			else
			{
				selectedTab=null;
				$(".tab").removeClass("tabselected");
			}
			$("#tab").val(elid.substr(3));
		});
		$("input#ipStart").bind("focus", function()
		{
			$(this).val("");
		});
		$("input#ipEnd").bind("focus", function()
		{
			$(this).val("");
		});
		$("input#ipStart").bind("blur", function()
		{
			$(this).val("IP Start");
		});
		$("input#ipEnd").bind("blur", function()
		{
			$(this).val("IP End");
		});
	<?php
	if (!isset($_REQUEST["tab"]))
		echo 'selectedTab = "tab1";
				$("#tab1").addClass("tabselected");
				$("#tab1box").css("display","");';
	else
		echo 'selectedTab = "tab'.intval($_REQUEST["tab"]).'";
				$("#tab'.intval($_REQUEST["tab"]).'").addClass("tabselected");
				$("#tab'.intval($_REQUEST["tab"]).'box").css("display","");';
	?>
	});
	function doDeleteIP(id, t){if(confirm("Are you sure you want to delete this IP?")){window.location='<?php echo $modulelink;?>&op=delip&id='+id+'&tab='+t;}};
	function doDeleteVLAN(id, t, vlan){if(confirm("Are you sure you want to delete this VLAN?")){window.location='<?php echo $modulelink;?>&op=delvlan&id='+id+'&vlan='+vlan+'&tab='+t;}};
	iBox.setPath("ibox/");iBox.fade_in_speed=5;iBox.fade_out_speed=5;
	</script>
	<?php

	/* initialization */
	$editable = 0;

	// permission to configure server required for editing
	$permid = 66;
	if (mysql_num_rows(mysql_query("SELECT permid FROM tbladmins JOIN tbladminperms ON tbladmins.roleid = tbladminperms.roleid WHERE permid=".$permid." AND tbladmins.id=". intval($_SESSION["adminid"]).";")))
		$editable = 1;

	$dc = array();
	$result = mysql_query("SELECT id, val FROM mod_config WHERE config='data_center' AND module='server_manager';");
	while ($tmp = mysql_fetch_assoc($result))
	{
		$dc[$tmp["id"]] = $tmp["val"];
	}

	$switch = array();
	$result = mysql_query("SELECT id, val FROM mod_config WHERE config='switch' AND module='server_manager';");
	while ($tmp = mysql_fetch_assoc($result))
	{
		$switch[$tmp["id"]] = $tmp["val"];
	}

	$result = mysql_query("SELECT mod_vlanmanager.* FROM mod_vlanmanager;");
	$vlans = array();
	$vlandcs = array();
	while ($tmp = mysql_fetch_assoc($result))
	{
		$vlans[$tmp['id']] = $tmp['vlanid'];
		$vlandcs[$tmp['id']] = $dc[$tmp['dcid']];
	}

	/* process post/get request */
	if (isset($_REQUEST["op"]) && $editable)
	{
		unset($error_msg);
		// add new ip address
		if (($_REQUEST["op"]=="addip") && isset($_REQUEST["ip"]) && isset($_REQUEST["vlan"]) && isset($_REQUEST["switch"]) && isset($_REQUEST["port"]) && (filter_var(trim($_REQUEST["ip"]), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)))
		{
			$action = "Added IP ".trim($_REQUEST["ip"])." in VLAN ".$vlans[intval($_REQUEST["vlan"])]." at port ".$_REQUEST["port"]." in switch ".$switch[intval($_REQUEST["switch"])]." with comment ".$_REQUEST["comment"];
			
			if (!mysql_query("INSERT INTO mod_ipmanager (id, ipaddress, vlan, switch, port, hostingid, comment) VALUES ('', '".mysql_real_escape_string(trim($_REQUEST["ip"]))."',".intval($_REQUEST["vlan"]).", '".intval($_REQUEST["switch"])."', ".intval($_REQUEST["port"]).", ".intval($_REQUEST["hostingid"]).", '".mysql_real_escape_string($_REQUEST["comment"])."');") || !mysql_query("INSERT INTO mod_history (id, tid, uid, logtime, action, type) VALUES ('', LAST_INSERT_ID(), '".intval($_SESSION["adminid"])."', NOW(), '".mysql_real_escape_string($action)."', 'iplog');"))
				$error_msg = mysql_error();
		}
		// delete ip address
		else if (($_REQUEST["op"]=="delip") && isset($_REQUEST["id"]))
		{
			$ip_before_delete = mysql_fetch_array(mysql_query("SELECT id, ipaddress, vlan, switch, port, hostingid, comment FROM mod_ipmanager WHERE  id = ".intval($_REQUEST["id"]).";"));
			
			if(count($ip_before_delete) > 0)
				if (!mysql_query( "INSERT INTO mod_history (id, tid, uid, logtime, action, type) VALUES ('' , ".intval($_REQUEST["id"]).", '".intval($_SESSION["adminid"])."', NOW(), 'Deleted IP: <a href=".$ajax_url."?op=get_history&type=ip&id=".intval($_REQUEST["id"])." rel=\'ibox&width=70%\' title=\'History\' alt=\'History\' >".$ip_before_delete['ipaddress']."</a> ', 'deleteip');") );
						$error_msg = mysql_error();
		
			if (!mysql_query("DELETE FROM mod_ipmanager WHERE id='".intval($_REQUEST["id"])."';"))
				$error_msg = mysql_error();
		}
		// edit ip address
		else if (($_REQUEST["op"]=="editip") && isset($_REQUEST["id"]) && isset($_REQUEST["ip"]) && isset($_REQUEST["vlan"]) && isset($_REQUEST["switch"]) && isset($_REQUEST["port"]) && (filter_var(trim($_REQUEST["ip"]), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)))
		{
			$ip_before_update = mysql_fetch_array(mysql_query("SELECT * FROM mod_ipmanager WHERE  id = ".intval($_REQUEST["id"]).";"));
				
			if (!mysql_query("UPDATE mod_ipmanager SET ipaddress='".mysql_real_escape_string($_REQUEST["ip"])."', vlan=".intval($_REQUEST["vlan"]).", switch='".intval($_REQUEST["switch"])."', port=".intval($_REQUEST["port"]).", hostingid=".intval($_REQUEST["hostingid"]).", comment='".mysql_real_escape_string($_REQUEST["comment"])."' WHERE id='".intval($_REQUEST["id"])."';") )
				$error_msg = mysql_error();
			
			//is update new IP info
			if ($ip_before_update['switch'] != intval($_REQUEST["switch"]) || $ip_before_update['vlan'] != intval($_REQUEST['vlan'])  || $ip_before_update['ipaddress'] != $_REQUEST['ip'] || $ip_before_update['port'] != intval($_REQUEST["port"]) || $ip_before_update['hostingid'] != intval($_REQUEST["hostingid"]) || $ip_before_update['comment'] != mysql_real_escape_string($_REQUEST["comment"])) 
			{			
				$insertHistory = "INSERT INTO mod_history (id, logtime,  tid, uid, type, action) VALUES ('', NOW(), ".intval($_REQUEST["id"]).", ".intval($_SESSION["adminid"]).", 'iplog',  ' Update ";
				if ($ip_before_update['switch'] != intval($_REQUEST["switch"]) ) 
				{
					$insertHistory .= "Switch: ".$switch[$ip_before_update['switch']]." to ".$switch[intval($_REQUEST["switch"])];
				}
				if ($ip_before_update['vlan'] != intval($_REQUEST["vlan"]))
				{
					$insertHistory .= " Vlan: ".$ip_before_update['vlan']." to ".intval($_REQUEST["vlan"]);
				}
				if ($ip_before_update['ipaddress']  != $_REQUEST['ip'])
				{
					$insertHistory .= " IPAddress: ".$ip_before_update['ipaddress']." to ".mysql_real_escape_string($_REQUEST['ip']);
				}
				if ($ip_before_update['port'] != intval($_REQUEST["port"]))
				{
					$insertHistory .= " Port: ".$ip_before_update['port']." to ".intval($_REQUEST["port"]);
				}
				if ($ip_before_update['hostingid'] != intval($_REQUEST["hostingid"])) 
				{
					$insertHistory .= " Hostingid: ".$ip_before_update['hostingid']." to ".intval($_REQUEST["hostingid"]);
				}
				if ($ip_before_update['comment'] != mysql_real_escape_string($_REQUEST["comment"]))
				{			
					$insertHistory .= ' Comment: "'.$ip_before_update['comment'].'" to "'.mysql_real_escape_string($_REQUEST["comment"]).' " ';
				}
				$insertHistory .= " ') ";
				
				if(!mysql_query($insertHistory))
					$error_msg = mysql_error();
				}			
		}
		// add new vlan
		else if (($_REQUEST["op"]=="addvlan") && isset($_REQUEST["vlanid"]) && isset($_REQUEST["dcid"]) && isset($_REQUEST["netmask"]) && isset($_REQUEST["gateway"])
		&& (filter_var(trim($_REQUEST["netmask"]), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) && (filter_var(trim($_REQUEST["gateway"]), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)))
		{
			//check exist vlan
			$existedVlan =  mysql_num_rows(mysql_query("SELECT vlanid FROM mod_vlanmanager WHERE vlanid =".intval($_REQUEST["vlanid"]))) == 0;
			
			if ($existedVlan)
			{		
				//add new vlan
				if (!mysql_query("INSERT INTO mod_vlanmanager (id, vlanid, dcid, netmask, gateway, comment) VALUES (NULL, '".intval($_REQUEST["vlanid"])."', '".intval($_REQUEST["dcid"])."', '".mysql_real_escape_string(trim($_REQUEST["netmask"]))."', '".mysql_real_escape_string(trim($_REQUEST["gateway"]))."', '".mysql_real_escape_string($_REQUEST["comment"])."');") || !mysql_query("INSERT INTO mod_history(id, tid, uid, logtime, action, type) VALUES('', LAST_INSERT_ID(), '".intval($_SESSION["adminid"])."' ,  NOW(), 'Added  new VLAN \'".intval($_REQUEST["vlanid"])."\'', 'vlanlog'  )"))				
					$error_msg = mysql_error();
				else
				{ 		
					//get last insert vlanid
					$insert_id = mysql_insert_id();
						
					//add new IPs 			
					if (($_REQUEST['ipStart']) && ($_REQUEST['ipEnd']) && ($_REQUEST['ipEnd'] > $_REQUEST['ipStart']) && ($_REQUEST['ipEnd']<255) && ($_REQUEST['ipStart']>0))
					{
						$tmp = get_ip_first3_oct($_REQUEST["gateway"]);
						for ($i = $_REQUEST['ipStart']; $i <= $_REQUEST['ipEnd']; $i++)
						{									
							$newIP = $tmp.'.'.$i;
							if (!mysql_query("INSERT INTO mod_ipmanager (id, ipaddress, vlan) VALUES ('', '".mysql_real_escape_string($newIP)."','".$insert_id."');") || !mysql_query("INSERT INTO mod_history (id, tid, uid, logtime, action, type) VALUES ('', LAST_INSERT_ID(), '".intval($_SESSION["adminid"])."', NOW(), 'Added IP ".mysql_real_escape_string($newIP)." to VLAN ".intval($_REQUEST["vlanid"])." (id ".$insert_id.")', 'iplog');"))
							{
								$error_msg = mysql_error();
								break;
							}
						}
					}
					else 
						$error_msg = "Invalid IP range!";
				}
			}
			else
				$error_msg = "VLAN existed!";
		}
		// delete vlan
		else if (($_REQUEST["op"]=="delvlan") && isset($_REQUEST["id"]))
		{		
			$rs = mysql_query("SELECT * FROM mod_ipmanager WHERE vlan = ".intval($_REQUEST["id"]).";");
			if (mysql_num_rows($rs))
				$error_msg = "There are IPs in this VLAN ".$_REQUEST["id"].", please remove them first!";	
			else
			{			
				$tmpRs = mysql_fetch_array(mysql_query("SELECT * FROM mod_vlanmanager WHERE id = ".intval($_REQUEST["id"]).";"));
				
				if(!mysql_query("INSERT INTO mod_history (id, tid, uid, logtime, type, action) VALUES('', ".$tmpRs['id'].", ".intval($_SESSION["adminid"]).", NOW(), 'deletevlan', 'Delete Vlan (id: ".$tmpRs['id'].") DC: ".$dc[$tmpRs['dcid']]." NetMask: ".$tmpRs['netmask']." Gateway: ".$tmpRs['gateway']." Comment: ".mysql_real_escape_string($tmpRs['comment'])."');"))
					$error_msg = mysql_error();	

				if (!mysql_query("DELETE FROM mod_vlanmanager WHERE id='".intval($_REQUEST["id"])."';"))
					$error_msg = mysql_error();
			}		
		}
		// edit vlan
		else if (($_REQUEST["op"]=="editvlan") && isset($_REQUEST["id"]) && isset($_REQUEST["vlanid"]) && isset($_REQUEST["dcid"]) && isset($_REQUEST["netmask"]) && isset($_REQUEST["gateway"])
		&& (filter_var(trim($_REQUEST["netmask"]), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) && (filter_var(trim($_REQUEST["gateway"]), FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)))
		{
			$vlanid_before_update = mysql_fetch_array(mysql_query("SELECT  * FROM mod_vlanmanager WHERE id =".intval($_REQUEST["id"])));
			
			if (!mysql_query("UPDATE mod_vlanmanager SET vlanid='".intval($_REQUEST["vlanid"])."', dcid='".intval($_REQUEST["dcid"])."', netmask='".mysql_real_escape_string(trim($_REQUEST["netmask"]))."', gateway='".mysql_real_escape_string(trim($_REQUEST["gateway"]))."', comment='".mysql_real_escape_string($_REQUEST["comment"])."' WHERE id='".intval($_REQUEST["id"])."';") )
				$error_msg = mysql_error();
			
			//is update new VLAN
			if ($vlanid_before_update['vlanid'] != intval($_REQUEST["vlanid"]) || $vlanid_before_update['netmask'] != mysql_real_escape_string(trim($_REQUEST["netmask"])) || $vlanid_before_update['gateway'] != mysql_real_escape_string(trim($_REQUEST["gateway"])) || $vlanid_before_update['dcid'] != intval($_REQUEST["dcid"]) || $vlanid_before_update['comment'] != mysql_real_escape_string($_REQUEST["comment"]))
			{
				$insertHisroty = "INSERT INTO mod_history(id, logtime, tid, uid, type, action) VALUES('' , NOW(), ".intval($_REQUEST["id"]).", ".intval($_SESSION["adminid"]).", 'updatevlanlog', 'Update ";
				
				if ($vlanid_before_update['vlanid'] != intval($_REQUEST["vlanid"]))
				{
					$insertHisroty .= "VLAN (id: ".intval($_REQUEST["id"]).") from ".$vlanid_before_update['vlanid']." to ".intval($_REQUEST["vlanid"]);
				}
				if ($vlanid_before_update['netmask'] != mysql_real_escape_string(trim($_REQUEST["netmask"])))
				{
					$insertHisroty .= " Netmask from: ".$vlanid_before_update['netmask']." to ".mysql_real_escape_string(trim($_REQUEST["netmask"]));
				}
				if ($vlanid_before_update['gateway'] != mysql_real_escape_string(trim($_REQUEST["gateway"])) )
				{
					$insertHisroty .= " Gateway from ".$vlanid_before_update['gateway']." to ".mysql_real_escape_string(trim($_REQUEST["gateway"]));
				}
				if ($vlanid_before_update['dcid'] != intval($_REQUEST["dcid"]))
				{
					$insertHisroty .= " DC from: ".$dc[$vlanid_before_update['dcid']]." to ".$dc[intval($_REQUEST["dcid"])];
				}
				if ($vlanid_before_update['comment'] != mysql_real_escape_string($_REQUEST["comment"]))
				{
					$insertHisroty .= ' Comment from "'.$vlanid_before_update['comment'].'" to "'.mysql_real_escape_string($_REQUEST["comment"]);
				}
				$insertHisroty .= " ')";
				if (mysql_query($insertHisroty))
					$error_msg = mysql_error();
			}
		}
	}

	/* VLAN tab*/
	$tabs["VLAN Manager"] = '<div class="tablebg">
									<table class="datatable" width="100%" border="0" cellspacing="1" cellpadding="3">
									<tr><th>VLAN ID</th><th>Datacenter</th><th>Netmask</th><th>Gateway</th><th>Comment</th><th>IPs</th><th colspan="3"></th></tr>';
									
	$result = mysql_query("SELECT mod_vlanmanager.*, COUNT(mod_ipmanager.id) as ipcount FROM mod_vlanmanager LEFT JOIN mod_ipmanager ON mod_vlanmanager.id = mod_ipmanager.vlan GROUP BY mod_vlanmanager.id ORDER BY mod_vlanmanager.vlanid ASC;");
	$tmpout = "";
	$vlans = array();
	$vlandcs = array();
	while ($tmp = mysql_fetch_assoc($result))
	{
		$vlans[$tmp['id']] = $tmp['vlanid'];
		$vlandcs[$tmp['id']] = $dc[$tmp['dcid']];
										
		$tabs["VLAN Manager"] .= '<tr onmouseover="this.className=\'over\'" onmouseout="this.className=\'norm\'"><td>'.$tmp['vlanid'].'</td>
										<td>'.$dc[$tmp['dcid']].'</td><td>'.$tmp['netmask'].'</td>
										<td>'.$tmp['gateway'].'</td><td>'.$tmp['comment'].'</td><td>'.$tmp['ipcount'].'</td><td colspan="3">
										<a href="'.$ajax_url.'?op=get_history&type=vlan&id='.$tmp['id'].'" rel="ibox&width=70%" title="History"><img src="images/info.gif" width="16" height="16" border="0" alt="History"></a>&nbsp;';
										
		if ($editable)
			$tabs["VLAN Manager"] .= '&nbsp;<a href="#vlan'.$tmp["id"].'" rel="ibox&width=70%" title="Edit"><img src="images/edit.gif" width="16" height="16" border="0" alt="Edit"></a>&nbsp;
										&nbsp;<a href="#" onClick="doDeleteVLAN(\''.$tmp["id"].'\',\''.(count($tabs)-1).'\',\''.$tmp['vlanid'].'\')"><img src="images/delete.gif" width="16" height="16" border="0" alt="Delete"></a>';
		$tabs["VLAN Manager"] .= '</td></tr>';
		$tmpout .= '<div id="vlan'.$tmp["id"].'" style="display:none;" class="tablebg">
						<form method="post" action="'.$modulelink.'">
						<table class="datatable" width="100%" border="0" cellspacing="1" cellpadding="3">
							<tr><th>VLAN ID</th><th>Datacenter</th><th>Netmask</th><th>Gateway</th><th>Comment</th><th></th></tr>
								<input type="hidden" name="op" value="editvlan">
								<input type="hidden" name="id" value="'.$tmp["id"].'">
								<input type="hidden" name="tab" value="'.(count($tabs)-1).'">
							<tr>
								<td><input type="text" size="20" name="vlanid" value="'.$tmp["vlanid"].'"></td>
								<td><select name="dcid">';
		foreach ($dc as $id => $d)
		{
			$tmpout .= "<option value='".$id."'";
			if ($tmp['dcid']==$id)
				$tmpout .= " selected";
			$tmpout .= ">".$d;
		}
			
		$tmpout .= '</select></td>
								<td><input type="text" size="20" name="netmask" value="'.$tmp["netmask"].'"></td>
								<td><input type="text" size="20" name="gateway" value="'.$tmp["gateway"].'"></td>
								<td><input type="text" size="50" name="comment" value="'.$tmp["comment"].'"></td>
								<td><input type="submit" value="Save"></td><td></td><td></td></tr></table>
						</form>
					</div>';
	}

	if ($editable)
	{
		$tabs["VLAN Manager"] .=	'<form method="post" action="'.$modulelink.'">
																<input type="hidden" name="op" value="addvlan">
																<input type="hidden" name="tab" value="'.(count($tabs)-1).'">
										<tr>
											<td><input type="text" size="20" name="vlanid"></td>
											<td><select name="dcid">';
		foreach ($dc as $id => $d)
		{
			$tabs["VLAN Manager"] .= "<option value='".$id."'>".$d;
		}
		$tabs["VLAN Manager"] .= '</select></td>
										<td><input type="text" size="20" name="netmask"></td>
										<td><input type="text" size="20" name="gateway"></td>
										<td><input type="text" size="50" name="comment"></td>
										<td></td>
										<td><input type="text" name="ipStart" id="ipStart" size="5" maxlength="3" value="IP Start" /></td>
										<td><input type="text" name="ipEnd" id="ipEnd" size="5" maxlength="3" value="IP End" /></td>
										<td><input type="submit" value="Add New"></td></tr>
									</form>';
	}
	$tabs["VLAN Manager"] .= '<tr><td colspan="9" style="text-align: right; padding-right: 5px"><a href="'.$ajax_url.'?op=remove_history&type=deletevlan" alt="Remove History" title="Remove History" rel="ibox&width=70%">Remove</a></td></tr>';
	$tabs["VLAN Manager"] .= '</table></div>'.$tmpout;

	if (isset($error_msg) && ($error_msg!=""))
		$tabs["VLAN Manager"] .= "<div class='infobox'>".$error_msg."</div>";

	/* IP tab */
	$tabs["IP Manager"] = '<div class="tablebg">
									<table class="datatable" width="100%" border="0" cellspacing="1" cellpadding="3">';

	$orderby = 'mod_ipmanager.ipaddress';
	if (isset($_REQUEST['orderby']))
	{
		if ($_REQUEST['orderby']=='port')
			$orderby = 'mod_ipmanager.switch, mod_ipmanager.port';
		if ($_REQUEST['orderby']=='vlan')
			$orderby = 'mod_ipmanager.vlan, mod_ipmanager.switch, mod_ipmanager.port';
		if ($_REQUEST['orderby']=='status')
			$orderby = 'tblhosting.domainstatus';
	}

	$filter = '1';
	$filterurl = '';
	$stat = '';
	
	if (isset($_REQUEST['fip']) && intval($_REQUEST['fip']))
	{
		$filterurl .= '&fip='.intval($_REQUEST['fip']);
		$filter .= ' AND mod_ipmanager.ipaddress like \'%'.mysql_real_escape_string($_REQUEST['fip'])."%'";		
	}
	
	if (isset($_REQUEST['fvlan']) && intval($_REQUEST['fvlan']))
	{
		$filterurl .= '&fvlan='.intval($_REQUEST['fvlan']);
		$filter .= ' AND mod_ipmanager.vlan='.intval($_REQUEST['fvlan']);
		$stat .= ' in VLAN '.$vlans[intval($_REQUEST['fvlan'])];
	}

	if (isset($_REQUEST['fdc']) && intval($_REQUEST['fdc']))
	{
		$filterurl .= '&fdc='.intval($_REQUEST['fdc']);
		$filter .= ' AND mod_vlanmanager.dcid='.intval($_REQUEST['fdc']);
		$stat .= ' in DC '.$dc[intval($_REQUEST['fdc'])];
	}

	if (isset($_REQUEST['fswitch']) && intval($_REQUEST['fswitch']))
	{
		$filterurl .= '&fswitch='.intval($_REQUEST['fswitch']);
		$filter .= " AND mod_ipmanager.switch='".intval($_REQUEST['fswitch'])."'";
		$stat .= ' in switch \''.$switch[intval($_REQUEST['fswitch'])].'\'';
	}

	if (isset($_REQUEST['fport']) && (intval($_REQUEST['fport'])>-1))
	{
		$filterurl .= '&fport='.intval($_REQUEST['fport']);
		$filter .= ' AND mod_ipmanager.port='.intval($_REQUEST['fport']);
		$stat .= ' in port '.intval($_REQUEST['fport']);
	}

	if (isset($_REQUEST['fstatus']) && in_array($_REQUEST['fstatus'], $status))
	{
		$filterurl .= '&fstatus='.$_REQUEST['fstatus'];
		if ($_REQUEST['fstatus']=='Free')
			$filter .= ' AND tblhosting.domainstatus IS NULL';
		else
			$filter .= " AND tblhosting.domainstatus='".mysql_real_escape_string($_REQUEST['fstatus'])."'";
		$stat .= ' is '.$_REQUEST['fstatus'];
	}

	$tabs["IP Manager"] .='<tr><th><a href="'.$modulelink.'&tab='.(count($tabs)-1).$filterurl.'">IP Address</a></th><th><a href="'.$modulelink.'&tab='.(count($tabs)-1).$filterurl.'&orderby=vlan">VLAN</a></th><th>Data Center</th><th><a href="'.$modulelink.'&tab='.(count($tabs)-1).$filterurl.'&orderby=port">Switch</a></th><th><a href="'.$modulelink.'&tab='.(count($tabs)-1).$filterurl.'&orderby=port">Port</a></th><th>Hosting ID</th><th>Hostname</th><th><a href="'.$modulelink.'&tab='.(count($tabs)-1).$filterurl.'&orderby=status">Status</a></th><th>Comment</th><th></th></tr>';

	$tabs["IP Manager"] .='<form method="POST" action="'.$modulelink.'"><input type="hidden" name="tab" value="'.(count($tabs)-1).'">
							<tr><td><input type="text" name="fip" maxlength="15"></td><td><select name="fvlan">
									<option value="0">Any VLAN';
	foreach ($vlans as $k => $v)
	{
		$tabs["IP Manager"] .= "<option value='".$k."'";
		if ($k==$_REQUEST['fvlan'])
			$tabs["IP Manager"] .= " selected";
		$tabs["IP Manager"] .= ">".$v;
	}
	$tabs["IP Manager"] .= '</select></td><td><select name="fdc">
									<option value="0">Any DC';
	foreach ($dc as $k => $v)
	{
		$tabs["IP Manager"] .= "<option value='".$k."'";
		if ($k==$_REQUEST['fdc'])
			$tabs["IP Manager"] .= " selected";
		$tabs["IP Manager"] .= ">".$v;
	}
	$tabs["IP Manager"] .= '</select></td>
								<td><select name="fswitch">
									<option value="0">Any switch';
	foreach ($switch as $k => $s)
	{
		$tabs["IP Manager"] .= "<option value='".$k."'";
		if ($k==intval($_REQUEST['fswitch']))
			$tabs["IP Manager"] .= " selected";
		$tabs["IP Manager"] .= ">".$s;
	}
	$tabs["IP Manager"] .= '</select></td>
								<td><select name="fport">
									<option value="-1">Any port';
	for($x=0;$x<25;$x++)
	{
		$tabs["IP Manager"] .= "<option value='".$x."'";
		if (isset($_REQUEST['fport']) && ($x==$_REQUEST['fport']))
			$tabs["IP Manager"] .= " selected";
		$tabs["IP Manager"] .= ">".$x;
	}
	$tabs["IP Manager"] .= '</select></td><td></td><td></td><td><select name="fstatus">
									<option value="0">Any status';
	foreach ($status as $s)
	{
		$tabs["IP Manager"] .= "<option value='".$s."'";
		if ($s==$_REQUEST['fstatus'])
			$tabs["IP Manager"] .= " selected";
		$tabs["IP Manager"] .= ">".$s;
	}
	$tabs["IP Manager"] .= '</select></td><td></td>
								<td><input type="submit" value="Filter" name="filterIP"></td>
							</tr></form>';

	if (isset($_REQUEST['filterIP']))
	{		
		$result = mysql_query("SELECT mod_ipmanager.*, tblhosting.userid, tblhosting.domain as domain, tblhosting.domainstatus, mod_vlanmanager.dcid FROM mod_ipmanager LEFT JOIN mod_vlanmanager ON mod_vlanmanager.id = mod_ipmanager.vlan LEFT JOIN tblhosting ON mod_ipmanager.hostingid=tblhosting.id WHERE (".$filter.") ORDER BY ".$orderby.";");
		$tmpout = "";
		$ipcount = mysql_num_rows($result);
		while ($tmp = mysql_fetch_assoc($result))
		{
			$tabs["IP Manager"] .='<tr onmouseover="this.className=\'over\'" onmouseout="this.className=\'norm\'"><td>'.$tmp['ipaddress'].'</td><td>'.$vlans[$tmp['vlan']].'</td><td>'.$vlandcs[$tmp['vlan']].'</td><td>'.$switch[$tmp['switch']].'</td><td>'.$tmp['port'].'</td>
										<td>';
			if (is_null($tmp['userid']))
				$tabs["IP Manager"] .='Free<td></td>';
			else
				$tabs["IP Manager"] .='<a href="clientshosting.php?userid='.$tmp['userid'].'&hostingid='.$tmp['hostingid'].'" target="_blank">'.$tmp['hostingid'].'</a></td><td>'.$tmp['domain'];
			$tabs["IP Manager"] .='</td><td>';
			if (is_null($tmp['domainstatus']))
				$tabs["IP Manager"] .='Free';
			else
				$tabs["IP Manager"] .=$tmp['domainstatus'];
			$tabs["IP Manager"] .='</td><td>'.$tmp['comment'].'</td><td>';

			if ($editable)
				$tabs["IP Manager"] .='<a href="'.$ajax_url.'?op=get_history&type=ip&id='.$tmp['id'].' " rel="ibox&width=70%" title="History"><img src="images/info.gif" width="16" height="16" border="0" alt="History"></a>&nbsp;<a href="#ip'.$tmp["id"].'" rel="ibox&width=70%" title="Edit"><img src="images/edit.gif" width="16" height="16" border="0" alt="Edit"></a>
										<a href="#" onClick="doDeleteIP(\''.$tmp["id"].'\',\''.(count($tabs)-1).'\')"><img src="images/delete.gif" width="16" height="16" border="0" alt="Delete"></a>';

			$tabs["IP Manager"] .='</td></tr>';

			$tmpout .= '<div id="ip'.$tmp["id"].'" style="display:none;" class="tablebg">
										<form method="post" action="'.$modulelink.'">
										<table class="datatable" width="100%" border="0" cellspacing="1" cellpadding="3">
										<tr><th>IP Address</th><th>VLAN</th><th>Switch</th><th>Port</th><th>Hosting ID</th><th>Comment</th><th></th></tr>
											<input type="hidden" name="op" value="editip">
											<input type="hidden" name="id" value="'.$tmp["id"].'">
											<input type="hidden" name="tab" value="'.(count($tabs)-1).'">
										<tr>
										<td><input type="text" size="20" name="ip" value="'.$tmp["ipaddress"].'"></td>
										<td><select name="vlan">';
			foreach ($vlans as $k => $v)
			{
				$tmpout .= "<option value='".$k."'";
				if ($k==$tmp['vlan'])
					$tmpout .= " selected";
				$tmpout .= ">".$v;
			}
			$tmpout .= '</select></td>
											<td><select name="switch">';
			foreach ($switch as $k => $s)
			{
				$tmpout .= "<option value='".$k."'";
				if ($k==$tmp['switch'])
					$tmpout .= " selected";
				$tmpout .= ">".$s;
			}
			$tmpout .= '</select></td>
											<td><select name="port">';
			for ($x=0;$x<25;$x++)
			{
				$tmpout .= "<option value='".$x."'";
				if ($x==$tmp['port'])
					$tmpout .= " selected";
				$tmpout .= ">".$x;
			}
			$tmpout .= '</select></td>
											<td><input type="text" size="10" name="hostingid" value="'.$tmp["hostingid"].'"></td>
											<td><input type="text" size="50" name="comment" value="'.$tmp["comment"].'"></td>
											<td><input type="submit" value="Save"></td></tr></table>
											</form>
										</div>';
		}
	}
	if ($editable)
	{
		$tabs["IP Manager"] .=	'<form method="post" action="'.$modulelink.'">
																<input type="hidden" name="op" value="addip">
																<input type="hidden" name="tab" value="'.(count($tabs)-1).'">
										<tr>
											<td><input type="text" size="20" name="ip"></td>
											<td><select name="vlan">';
		foreach ($vlans as $k => $v)
		{
			$tabs["IP Manager"] .= "<option value='".$k."'>".$v;
		}
		$tabs["IP Manager"] .= '</select></td><td></td>
										<td><select name="switch">';
		foreach ($switch as $k => $s)
		{
			$tabs["IP Manager"] .= "<option value='".$k."'>".$s;
		}
		$tabs["IP Manager"] .= '</select></td>
										<td><select name="port">';
		for ($x=0;$x<25;$x++)
		{
			$tabs["IP Manager"] .= "<option value='".$x."'>".$x;
		}
		$tabs["IP Manager"] .= '</select></td>
										<td><input type="text" size="10" name="hostingid"></td>
										<td></td><td></td>
										<td><input type="text" size="50" name="comment"></td>
										<td><input type="submit" value="Add New"></td></tr>
									</form>';
	}

	$tabs["IP Manager"] .= '<tr><td colspan="10" style="text-align: right; padding-right: 5px"><a href="'.$ajax_url.'?op=remove_history&type=deleteip" alt="Remove History" title="Remove History" rel="ibox&width=70%">Remove</a></td></tr>';						
	$tabs["IP Manager"] .= '</table>
							</div>'.$tmpout;
	$tabs["IP Manager"] .= '<br> Total '.$ipcount.' IPs'.$stat;
	if (isset($error_msg) && ($error_msg!=""))
		$tabs["IP Manager"] .= "<div class='infobox'>".$error_msg."</div>";

	/* display */
	echo '<div id="tabs">
					<ul>';
	$x = 0;
	foreach ($tabs as $t => $s)
	{
		echo	'<li id="tab'.$x++.'" class="tab"><a href="javascript:;">'.$t.'</a></li>';
	}
	echo   '</ul>
				</div>';
	$x = 0;
	foreach ($tabs as $t => $s)
	{
		echo '<div id="tab'.$x++.'box" class="tabbox">
						<div id="tab_content" style="text-align:left;">'.$s.'
						</div>
					</div>';
	}

	?>